package com.mayn.api;

import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import com.jd.open.api.sdk.request.etms.EtmsTraceGetRequest;
import com.jd.open.api.sdk.response.etms.EtmsTraceGetResponse;
import com.mayn.common.ShopEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class getJdTraceApi {
	static String SERVER_URL = "https://api.jd.com/routerjson";
	
	public static void main(String[] args) {
		String accessToken = "150bc063-2503-4375-bb42-3b3c7c7e57a8";
		String appKey = "E581FB50C9EC51529357965BB0D56AC9";
		String appSecret = "6bbd52c2661c45d8bab05003380fd260";
		
		
		ShopEntity se = new ShopEntity();
		se.setShopApiAccessToken(accessToken);
		se.setShopApiAppKey(appKey);
		se.setShopApiAppSecrect(appSecret);
		System.out.println(getJdTraceJson(se, "VE32506974275"));
		System.out.println(getJdTraceJson(se, "VE25677049882"));
	}

	public static String getJdTraceJson(ShopEntity se, String LogisticCode) {
		LogisticCode = LogisticCode.toUpperCase();
		String jdTraceStr = getOrigJdTraceJson(se, LogisticCode);
		JSONObject jo = JSONObject.fromObject(jdTraceStr);
		JSONObject newJson = new JSONObject();
		
		if (jo.containsKey("jingdong_etms_trace_get_responce")) {
			if(jo.getJSONObject("jingdong_etms_trace_get_responce").getString("trace_api_dtos").equals("[]")){
				if(LogisticCode.indexOf("-") >0){
					String code =  LogisticCode.substring(0, LogisticCode.indexOf("-"));
					jdTraceStr = getOrigJdTraceJson(se, code);
					jo = JSONObject.fromObject(jdTraceStr);
				}
			}
			if (!jo.getJSONObject("jingdong_etms_trace_get_responce").getString("trace_api_dtos").equals("[]")) {
				newJson.put("LogisticCode", LogisticCode);
				newJson.put("ShipperCode", "JD");
				newJson.put("Success", "true");

				JSONArray traceJson = new JSONArray();
				JSONArray oldTrace = jo.getJSONObject("jingdong_etms_trace_get_responce").getJSONArray("trace_api_dtos");
				if (oldTrace.size() > 0) {
					for (int i = 0; i < oldTrace.size(); i++) {
						JSONObject joItem = new JSONObject();
						joItem.put("AcceptStation", oldTrace.getJSONObject(i).getString("ope_remark"));
						joItem.put("AcceptTime", oldTrace.getJSONObject(i).getString("ope_time"));
						joItem.put("Remark", oldTrace.getJSONObject(i).getString("ope_title"));
						traceJson.add(joItem);
					}
					if (oldTrace.getJSONObject(oldTrace.size() - 1).getString("ope_title").equals("妥投")
					 ||	(oldTrace.size()>2 && oldTrace.getJSONObject(oldTrace.size() - 2).getString("ope_title").equals("妥投"))
					 ||	(oldTrace.size()>3 && oldTrace.getJSONObject(oldTrace.size() - 3).getString("ope_title").equals("妥投"))
					) {
						newJson.put("State", 3);
					} else if((oldTrace.size()>2 && oldTrace.getJSONObject(oldTrace.size() - 2).getString("ope_title").equals("拒收"))
							||oldTrace.getJSONObject(oldTrace.size() - 1).getString("ope_remark").contains("拒收")
							||oldTrace.getJSONObject(oldTrace.size() - 1).getString("ope_remark").contains("换单完成") ){
						newJson.put("State", 4);
					} else {
						newJson.put("State", 2);
					}
					newJson.put("Traces", traceJson);
				}
			} else {
				newJson.put("Success", "false");
			}
			return newJson.toString();
		}
		return "{}";
	}

	public static String getOrigJdTraceJson(ShopEntity se, String waybillCode) {
		String accessToken = se.getShopApiAccessToken();
		String appKey = se.getShopApiAppKey();
		String appSecret = se.getShopApiAppSecrect();
		EtmsTraceGetResponse response = null;
		
		JdClient client = new DefaultJdClient(SERVER_URL, accessToken, appKey, appSecret);
		EtmsTraceGetRequest request = new EtmsTraceGetRequest();
		request.setWaybillCode(waybillCode);
		try {	
			response = client.execute(request);			
		} catch (JdException e) {
			try {	
				response = client.execute(request);			
			} catch (JdException e1) {
				try {	
					response = client.execute(request);			
				} catch (JdException e2) {
					e2.printStackTrace();
					return "{}";
				}
			}
		}
		if(response!=null){
			return response.getMsg();
		}else{
			return "{}";
		}
	}

	
}
